# Maintainer: Daurnimator <daurnimator@archlinux.org>

pkgname=('grafana-agent' 'grafana-agentctl' 'grafana-agent-crow')
pkgver=0.38.0
pkgrel=1
pkgdesc='Grafana Agent is a telemetry collector for sending metrics, logs, and trace data to the opinionated Grafana observability stack'
url='https://github.com/grafana/agent'
arch=('loong64' 'x86_64')
license=('Apache')
depends=('glibc')
makedepends=('go' 'systemd' 'yarn')
options=('!lto')
source=($pkgname-$pkgver.tar.gz::https://github.com/grafana/agent/archive/v$pkgver.tar.gz
        grafana-agent.sysusers grafana-agent.service grafana-agent.tmpfiles)
sha512sums=('8aac40ddcaaa9fa45a3d6b79e35227ba372ce37812add1453b267e8fe60c185088fb475b0b7664ee4fef441de14cba0c3f493347ea284cbc2e760a8c312db9ef'
            '1e58f6273562fd6ddeae41bb6d223230ed301199af6bd2f85fa3d2c1e6352952c600cd4488ad769069519c42b6863be84aaa2c93c88f696e5a0bedbb93758d0e'
            '1fb0bdcbf1bf7b11e83111a998f05d7fff0dda936054d17d061da97078be1ddfc4b90f0ea0578932049fc3eb4ccd7f61492e2395d1b1fcbc4ad908bb26c5623c'
            '3a4c4896b2454272b5a2d53ba5aa7009fa5b42a573fef521afb5d9712e53119539c4195d2b149d04d086c5fa197ccf7dc9bbdc407efb55514e3c92b994121fa8')

prepare() {
  cd "agent-$pkgver"

  (
    cd web/ui
    yarn install --purelockfile --cache-folder "${srcdir}/yarn-cache"
  )

  export GOPATH="${srcdir}"
  go mod download
}

build() {
  cd "agent-$pkgver"

  (
    cd web/ui
    PATH="$PATH:./node_modules/.bin" yarn build
  )

  export CGO_LDFLAGS="${LDFLAGS}"
  export CGO_CFLAGS="${CFLAGS}"
  export CGO_CPPFLAGS="${CPPFLAGS}"
  export CGO_CXXFLAGS="${CXXFLAGS}"
  export GOPATH="${srcdir}"
  export GOPROXY=off
  for path in cmd/grafana-agent cmd/grafana-agentctl tools/crow; do
    echo "Building $path"
    go build \
      -trimpath \
      -buildmode=pie \
      -mod=readonly \
      -modcacherw \
      -tags "builtinassets promtail_journal_enabled" \
      -ldflags " \
         -X github.com/grafana/agent/pkg/build.Version=$pkgver
         -X github.com/grafana/agent/pkg/build.BuildDate=$(date -u +'%Y-%m-%dT%H:%M:%SZ' --date=@${SOURCE_DATE_EPOCH})
         -compressdwarf=false
         -linkmode external
         -extldflags \"${LDFLAGS}\"" \
      "./$path"
  done
}

check() {
  cd "agent-$pkgver"

  ./grafana-agent -version
  ./grafana-agentctl --version

  # Skip `Test_tlsListener` due to https://github.com/grafana/agent/issues/4919
  go test -v ./pkg/{agentctl,config,crow,flow,logs,metrics,runner,server,traces}/ \
    -skip Test_tlsListener
}

package_grafana-agent() {
  backup=('etc/grafana-agent.yaml')

  cd "agent-$pkgver"

  install -Dm755 -t "$pkgdir/usr/bin" grafana-agent
  install -Dm644 packaging/grafana-agent/grafana-agent.yaml "$pkgdir/etc/grafana-agent.yaml"

  install -Dm644 "$srcdir/grafana-agent.sysusers" "$pkgdir/usr/lib/sysusers.d/grafana-agent.conf"
  install -Dm644 "$srcdir/grafana-agent.tmpfiles" "$pkgdir/usr/lib/tmpfiles.d/grafana-agent.conf"
  install -Dm644 "$srcdir/grafana-agent.service" "$pkgdir/usr/lib/systemd/system/grafana-agent.service"
}

package_grafana-agentctl() {
  pkgdesc="provides utilities for interacting with Grafana Agent"

  cd "agent-$pkgver"

  install -Dm755 -t "$pkgdir"/usr/bin grafana-agentctl
}

package_grafana-agent-crow() {
  pkgdesc="A correctness checker tool that validates that scraped metrics are delivered to a remote_write endpoint."

  cd "agent-$pkgver"

  install -Dm755 crow "$pkgdir"/usr/bin/grafana-agent-crow
}
